<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src=".././static/js/public-head.js"></script>
</head>
<body>
<div class="layui-card " >
    <div class="layui-card-body" >
        <div class="layui-tab" lay-filter="sysNotice-tab">
            <ul class="layui-tab-title">
                <li class="layui-this" lay-id="0">公告<span></span></li>
                <li lay-id="1">私信<span></span></li>
            </ul>
            <div class="layui-tab-content" >
                <div class="layui-tab-item layui-show" >
                     <table class="layui-hide" id="sysNotice-public-table" lay-filter="sysNotice-public-table"></table>
                </div>
                <div class="layui-tab-item">
                     <table class="layui-hide" id="sysNotice-private-table" lay-filter="sysNotice-private-table"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">

    var editparam={
        height: 180,
        tool: [
            'strong' //加粗
            ,'italic' //斜体
            ,'underline' //下划线
            ,'del' //删除线
            ,'|' //分割线
            ,'left' //左对齐
            ,'center' //居中对齐
            ,'right' //右对齐
            ,'link' //超链接
            ,'unlink' //清除链接
            ,'face' //表情
        ]
    }

    layui.config({
        base: basePath+'/layui/lay/modules/'
    }).extend({
        formSelects: 'formSelects-v4'
    }).use([ 'table','form','formSelects',"element",'layedit','laytpl' ], function() {
        var $ = layui.$;
        var form = layui.form;
        var formSelects = layui.formSelects;
        var layer = layui.layer;
        var table = layui.table;
        var element = layui.element;
        var layedit = layui.layedit;
        var laytpl = layui.laytpl

        var layindex;

        var pub = {
            elem: '#sysNotice-public-table'
            ,url: basePath + '/sysNotice/index'
            ,where:{token:token,type:"0"}
            ,method:"post"
            ,toolbar:""
            ,title:"公告"
            ,defaultToolbar:['filter', 'print']
            ,cols: [[
                 {title: '请选择', width:80,type:"radio",fixed:'left' }
                ,{field:'TITLE', title: '主题',fixed:'left', event: 'setSign',sort:true,templet: function (d) {
                    var flag = ""
                    if(d.STATE == "1"){
                        flag = '<a href="javascript:;"><span class="layui-badge layui-badge">'+d.TITLE+'</span></a>'
                    }else{
                        flag = d.TITLE
                    }
                    return flag
                } }

                ,{field:'DATE_CREATED', title: '发布时间' ,sort:true }
                ,{field:'CREATOR', title: '发布人' }
            ]]
            ,page: true
            ,height: 'full-130'
        }



        var pri ={
            elem: '#sysNotice-private-table'
            ,url: basePath + '/sysNotice/index'
            ,where:{token:token,type:"1"}
            ,method:"post"
            ,toolbar: ""
            ,title:"私信"
            ,defaultToolbar:['filter', 'print']
            ,cols: [[
                {title: '请选择', width:80,type:"radio",fixed:'left' },
                 {field:'TITLE', title: '主题',fixed:'left',sort:true, event: 'setSign' ,
                    templet: function (d) {
                        var flag = ""
                        var noticeCount = 0

                        if( typeof d.NOTICE_COUNT == "number"){
                            noticeCount += d.NOTICE_COUNT
                        }
                        if(noticeCount != 0){
                            noticeCount =  noticeCount >100 ?"99+": noticeCount
                            flag = '<a href="javascript:;">'+d.TITLE+'&nbsp;<span class="layui-badge">'+ noticeCount +'</span></a>'
                        }else{
                            flag = '<a href="javascript:;">'+d.TITLE+'</a>'
                        }
                        return flag
                    }}
                ,{field:'DATE_CREATED', title: '发布时间' ,sort:true }
                ,{field:'CREATOR_NAME', title: '发布人' ,sort:true }
                ,{field:'TO_USER_NAME', title: '接收人' ,sort:true }
            ]]
            ,page: true
            ,height: 'full-130'
        };

        //设置私信通知
        $.getJSON(basePath+"/sysNotice/setPrivateNotice",{type:"1"},function(rst){
            if(rst.code == 0 && rst.data !=0){
                var num = rst.data>99?"99+":rst.data
                $('li[lay-id="1"]').find("span").addClass("layui-badge").html(num)
            }
        });

        $.getJSON(basePath+"/sysNotice/setPrivateNotice",{type:"0"},function(rst){
            if(rst.code == 0 && rst.data !=0){
                var num = rst.data>99?"99+":rst.data
                $('li[lay-id="0"]').find("span").addClass("layui-badge").html(num)
            }
        });

        element.on('tab(sysNotice-tab)', function(data){
           // $('li[lay-id="1"]').find("span").removeClass("layui-badge").empty()
        });

        $.ajax(basePath+"/function/getFuncOptsByAuthList"+urlParam,{
            //data:{token:localStorage.getItem("token")},
            dataType:'json',
            //async: false,
            success:function(rst){
                if(rst.code == 0){
                    var bt = rst.data;
                    if(bt.length !=0){
                        var btstr = "";
                        for(var i=0;i<bt.length;i++){
                            var buData = bt[i]
                            btstr += "<button class='layui-btn "+buData.STYLE+"' lay-event='"+buData.OPERATE_NO+"'  _type='"+buData.TYPE+"' _action='"+buData.ACTION+"' >" +
                                    "<i class='layui-icon "+buData.ICON+"'></i>"+buData.OPERATE_NAME+"</button>"
                        }

                        pub.toolbar =  "<div><div class='layui-btn-group'>"+btstr +"</div></div>"

                    }else{
                        pub.cols[0].splice(0, 1);
                    }
                }
                pri.toolbar =  "<div><div class='layui-btn-group'>"+$("#sysNotice-private-bar").html()+ "</div></div>"

                table.render(pri)
                table.render(pub)
            }
        })

        table.on('tool(sysNotice-public-table)', function(obj){
                layer.open({
                    type: 1
                    ,title: "<ii class='layui-icon layui-icon-note'></ii> "+obj.data.TITLE //不显示标题栏
                    ,closeBtn: false
                    ,area: '500px;'
                    ,shade: 0.6
                    ,id: 'sysNotice-pb-tips' //设定一个id，防止重复弹出
                    ,btn: ['知道了']
                    ,btnAlign: 'c'
                    ,moveType: 1 //拖拽模式，0或者1
                    ,content: '<div style="padding: 50px; line-height: 22px; background-color:#e2e2e2; color: black; font-weight: 300;">'+obj.data.CONTENT||'</div>'
                });
        });



        table.on('tool(sysNotice-private-table)', function(obj){
            var id = obj.data.ID;
            var titleLabel = " 读信"
            var content = ""

            $.post(basePath+"/sysNotice/listAllNoticeById",{id:id,token:token},function(rst){
               // console.log(rst)
                if(rst.code != 0){
                    layer.msg(rst.msg);
                }else{
                    laytpl($("#sysNotice-private-read-dom").html()).render( rst.data, function(string){
                        layer.open({
                            type:1,
                            title:"<i class='layui-icon layui-icon-note'></i> "+titleLabel ,
                            content: string ,
                            area: ['700px', '500px'],
                            end : function(){
                                table.reload("sysNotice-private-table")
                            },
                            success :function(layero, index0){
                                form.on("submit(sysNotice-private-read-bt)",function(obj){
                                    $.post(basePath+"/sysNotice/update",{id:id},function(rst){
                                        if(rst.code != 0){
                                            layer.msg(rst.msg);
                                        }else{
                                            layer.close(index0);
                                        }
                                    },"json")
                                    return false
                                })
                            }
                        })
                    });
                }
            },"json")
        });

        table.on("toolbar(sysNotice-public-table)",function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'updateReadAll':
                    $.post(basePath+"/sysNotice/update",{token:token,type:"0"},function(rst){
                        if(rst.code == 0){
                            table.reload("sysNotice-public-table")
                        }else{
                            layer.msg(rst.msg);
                        }
                    },"json")
                    break;
                case 'updateRead':
                    if(checkStatus.data.length == 0){
                        layer.msg("请先选中后操作")
                        return
                    }else{
                        var id = checkStatus.data[0].ID
                        $.post(basePath+"/sysNotice/update",{id:id,token:token},function(rst){

                            if(rst.code == 0){
                                table.reload("sysNotice-public-table")
                            }else{
                                layer.msg(rst.msg);
                            }
                        },"json")
                    }
                    break;
                case 'del':
                    if(checkStatus.data.length == 0){
                        layer.msg("请先选中后操作")
                        return
                    }else{
                        var id = checkStatus.data[0].ID
                        layer.confirm("确定要删除该公告吗？", {icon: 3, title:'提示'},function(index){
                            $.post(basePath+"/sysNotice/del",{id:id,token:token},function(rst){
                                if(rst.code == 0){
                                    table.reload("sysNotice-public-table")
                                }else{
                                    layer.msg(rst.msg);
                                }
                            },"json")
                            layer.close(index);
                        })
                    }
                    break;
                case "addPublicNotice":
                    var titleLabel = "发布公告";
                    layer.open({
                        type:1,
                        title:"<ii class='layui-icon layui-icon-note'></ii> "+titleLabel ,
                        content:  $("#sysNotice-public-form-dom").html() ,
                        area: ['700px', '500px'],
                        end : function(){
                            table.reload("sysNotice-public-table")
                        },
                        success :function(layero, index0){
                            var editindex1 = layedit.build('CONTENT',editparam );

                            layui.formSelects.config('org', {
                                searchUrl: basePath+'/user/selectOrgTreeData?token='+token,
                                linkageWidth: 180
                            });


                            form.on("submit(sysNotice-public-form-bt)",function(obj){
                                var param = obj.field
                                param.CONTENT = layedit.getContent(editindex1)
                                param.token =token;
                                $.post(basePath+"/sysNotice/add",param,function(rst){

                                    if(rst.code == 0){
                                        layer.close(index0)
                                    }else{
                                        layer.msg(rst.msg);
                                    }
                                },"json")
                                return false
                            })
                        }
                    })
                    break;
            };
        })

        table.on("toolbar(sysNotice-private-table)",function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'updateReadAll':
                    $.post(basePath+"/sysNotice/update",{token:token,type:"1"},function(rst){
                        if(rst.code == 0){
                            table.reload("sysNotice-private-table")
                        }else{
                            layer.msg(rst.msg);
                        }
                    },"json")
                    break;
                case 'updateRead':
                    if(checkStatus.data.length == 0){
                        layer.msg("请先选中后操作")
                        return
                    }else{
                        var id = checkStatus.data[0].ID
                        $.post(basePath+"/sysNotice/update",{id:id,token:token},function(rst){

                            if(rst.code == 0){
                                table.reload("sysNotice-private-table")
                            }else{
                                layer.msg(rst.msg);
                            }
                        },"json")
                    }
                    break;
                case 'del':
                    if(checkStatus.data.length == 0){
                        layer.msg("请先选中后操作")
                        return
                    }else{
                        var id = checkStatus.data[0].ID
                        layer.confirm("删除后，对方也将看不见!", {icon: 3, title:'提示'},function(index){
                            $.post(basePath+"/sysNotice/del",{id:id,token:token},function(rst){
                                if(rst.code == 0){
                                    table.reload("sysNotice-private-table")
                                }else{
                                    layer.msg(rst.msg);
                                }
                            },"json")
                            layer.close(index);
                        })
                    }
                    break;
                case "replyPrivateNotice":
                    if(checkStatus.data.length == 0){
                        layer.msg("请先选中后操作")
                        return
                    }else{
                        var id = checkStatus.data[0].ID
                        titleLabel = "回私信";
                        layer.open({
                            type:1,
                            title:"<ii class='layui-icon layui-icon-note'></ii> "+titleLabel ,
                            content:  $("#sysNotice-private-form-dom").html() ,
                            area: ['700px', '500px'],
                            end : function(){
                                table.reload("sysNotice-private-table")
                            },
                            success :function(layero, index0){
                                var editindex2 = layedit.build('CONTENT-P',editparam );
                                $("#PARENT_ID").val(id)
                                $("#TITLE").val("回复："+checkStatus.data[0].TITLE)

                                layui.formSelects.config('toUser', {
                                    searchUrl: basePath+'/user/getUserSelectList?token='+token,
                                    linkageWidth: 180,
                                    success: function(id, url, searchVal, result){
                                        var toUser = ""
                                        if(checkStatus.data[0].CREATOR==userNo){
                                            toUser = checkStatus.data[0].TO_USER;
                                        }else{
                                            toUser = checkStatus.data[0].CREATOR;
                                        }
                                        formSelects.value('toUser', [toUser]);
                                    }
                                    });

                                form.on("submit(sysNotice-private-form-bt)",function(obj){
                                    var param = obj.field
                                    param.CONTENT = layedit.getContent(editindex2)
                                    param.token =token;

                                    $.post(basePath+"/sysNotice/addPrivate",param,function(rst){
                                        if(rst.code == 0){
                                            layer.close(index0)
                                        }else{
                                            layer.msg(rst.msg);
                                        }
                                    },"json")
                                    return false
                                })
                            }
                        })
                    }
                    break;
                case "addPrivateNotice":
                    titleLabel = "写私信";
                    layer.open({
                        type:1,
                        title:"<ii class='layui-icon layui-icon-note'></ii> "+titleLabel ,
                        content:  $("#sysNotice-private-form-dom").html() ,
                        area: ['700px', '500px'],
                        end : function(){
                            table.reload("sysNotice-private-table")
                        },
                        success :function(layero, index0){
                            $("#PARENT_ID").val("")
                            $("#TITLE").val("")
                            var editindex2 = layedit.build('CONTENT-P',editparam );

                            layui.formSelects.config('toUser', {
                                searchUrl: basePath+'/user/getUserSelectList?token='+token,
                                linkageWidth: 180
                            });

                            form.on("submit(sysNotice-private-form-bt)",function(obj){
                                var param = obj.field
                                param.CONTENT = layedit.getContent(editindex2)
                                param.token =token;
                                $.post(basePath+"/sysNotice/addPrivate",param,function(rst){
                                    if(rst.code == 0){
                                        layer.close(index0)
                                    }else{
                                        layer.msg(rst.msg);
                                    }
                                },"json")
                                return false
                            })
                        }
                    })
                    break;
            };
        })

    });


</script>
</body>
<script type="text/html" id="sysNotice-private-bar">
    <button class='layui-btn layui-btn-sm' lay-event='del'  _type='ROW_BUTTON' _action='' >删除</button>
    <button class='layui-btn layui-btn-sm' lay-event='updateRead'  _type='ROW_BUTTON' _action='' >标记已读</button>
    <button class='layui-btn layui-btn-sm' lay-event='updateReadAll'  _type='ROW_BUTTON' _action='' >全部标记已读</button>
    <button class='layui-btn layui-btn-sm' lay-event='addPrivateNotice'  _type='ROW_BUTTON' _action='' >写信</button>
    <button class='layui-btn layui-btn-sm' lay-event='replyPrivateNotice'  _type='ROW_BUTTON' _action='' >回复</button>
</script>
</html>
<!--updateRead,updateReadAll,del,addPublicNoice,addPrivateNotice -->
<script type="text/html" id="sysNotice-public-form-dom">
    <div class="layui-card"  >
        <div class="layui-card-body"  >
            <form class="layui-form " action="" lay-filter="sysNotice-public-form">
                <input type="hidden" name="TYPE" value="0" >
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">主题</label>
                        <div class="layui-input-inline">
                            <input type="text" name="TITLE"  lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">有效期</label>
                        <div class="layui-input-inline">
                            <input type="text" name="REMAIN_DAY"  placeholder="提醒天数" lay-verify="number" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">通知范围</label>
                        <div class="layui-input-inline">
                            <select  name="orgId"   xm-select="org"  xm-select-search="" xm-select-height="36px"	>
                            </select>
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            不选则根据用户数据权限范围通知
                        </div>
                    </div>
                </div>
                <div class="layui-form-item ">
                        <textarea type="text"   id="CONTENT"  name="CONTENT" lay-verify="required"   autocomplete="off" class="layui-textarea">请填写公告内容</textarea>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn" lay-submit="" lay-filter="sysNotice-public-form-bt">发布</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</script>

<script type="text/html" id="sysNotice-private-form-dom">
    <div class="layui-card"  >
        <div class="layui-card-body"  >
            <form class="layui-form " action="" lay-filter="sysNotice-public-form">
                <input type="hidden" name="ID" value="" id="ID">
                <input type="hidden" name="PARENT_ID" value="" id="PARENT_ID">
                <input type="hidden" name="TYPE" value="1" >
                <div class="layui-form-item">

                        <label class="layui-form-label">主题</label>
                        <div class="layui-input-block"   >
                            <input type="text" name="TITLE" id="TITLE"  lay-verify="required" autocomplete="off" class="layui-input">
                        </div>

                </div>
                <div class="layui-form-item">

                        <label class="layui-form-label">私信对像</label>
                        <div class="layui-input-block" >
                            <select  name="toUser"   xm-select="toUser" lay-verify="required"  xm-select-search="" xm-select-height="36px"	>
                            </select>
                        </div>

                </div>
                <div class="layui-form-item ">
                    <textarea type="text"  name="CONTENT"  id="CONTENT-P"   autocomplete="off" class="layui-textarea"></textarea>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn" lay-submit="" lay-filter="sysNotice-private-form-bt">发送</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</script>


<script type="text/html" id="sysNotice-private-read-dom">
    {{# var fnBolck=function(state){
    if(state == "1"){
    return "<span class='layui-badge'>新</span>"
    }
    return ""
    }
    }}

    <form class="layui-form " >
        <div class="layui-card"  >
            <div class="layui-card-body"  >
                <fieldset class="layui-elem-field">
                     <blockquote class="layui-elem-quote" >
                       <b>{{fnBolck(d[0].STATE)}} 主题：《{{d[0].TITLE}}》 <label style="font-size: 14px">来自:{{d[0].USER_NAME}} {{d[0].DATE_CREATED}}</label></b>
                     </blockquote>
                    <div class="layui-field-box">
                        {{d[0].CONTENT}}
                        {{# for(var i=1; i < d.length ;i++){  }}
                        <fieldset class="layui-elem-field">
                            <legend>{{fnBolck(d[i].STATE)}} @{{d[i].TO_USER_NAME}}<label style="font-size: 12px;"> 来自:{{d[i].USER_NAME}} {{d[i].DATE_CREATED}} 的回复.</label></legend>
                            <div class="layui-field-box">{{d[i].CONTENT}}</div>
                        </fieldset>
                        {{#}}}
                    </div>
                </fieldset>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn" lay-submit="" lay-filter="sysNotice-private-read-bt">已阅</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</script>